<?php

namespace App\Module\Api\Controllers;
use App\Exceptions\Customize\ParamsException;
use App\Module\Api\Controllers\Request\BalanceWithdrawRequest;
use App\Module\Api\Validator\BalanceWithdrawValidator;
use App\System\Base\Services\TokenService;
use App\System\Cis\Repositories\CisBankAccountRepository;
use App\System\Cis\Repositories\CisUserRepository;
use App\System\Fina\Repositories\FinaIntegralRegRecordRepository;
use Illuminate\Support\Facades\DB;

/**
 *提现申请
 */
class BalanceWithdrawController extends Controller
{

    public function __construct()
    {
        $this->query = new BalanceWithdrawRequest();
        parent::__construct();

    }

    public function index()
    {
        (new BalanceWithdrawValidator())->check();
         DB::beginTransaction();
         $user_id = TokenService::getUserId();
         $master_id = TokenService::getMasterId();
         $user = CisUserRepository::getInstance();
         $master_info = $user->checkPayPassword($master_id,$this->query->payPassword);
         if(!$master_info)
         {
             throw new ParamsException([
                 'error_code' => '1000',
                 'message'    => '支付密码错误',
             ]);
         }
         $user_info = $user->getUser($user_id);
         if($user_info->integral < $this->query->amount)
         {
             throw new ParamsException([
                 'error_code' => '1000',
                 'message'    => '提现金额不能大于钱包积分余额',
             ]);
         }
         $bank = CisBankAccountRepository::getInstance();
         $bank_info = $bank->getOne($this->query->bankCardId,$master_id);
         if(!$bank_info)
         {
             throw new ParamsException([
                 'error_code' => '1000',
                 'message'    => '银行卡不存在',
             ]);
         }

         $res = $user->updateUserPurse($user_id,2,'integral',$this->query->amount);
         if(!$res)
         {
             throw new ParamsException([
                 'error_code' => '1000',
                 'message'    => '提现失败，服务内部异常',
             ]);
         }
         $bankAccountArray = ['id'=>$bank_info->id,'account_name'=>$bank_info->account_name,'account_branch_name'=>$bank_info->bank->bank_name.$bank_info->account_branch_name,'account_code'=>$bank_info->account_code,'user_id'=>$bank_info->user_id];
         $accountJson = json_encode($bankAccountArray,JSON_UNESCAPED_UNICODE);
         $cash = round($this->query->amount * 0.95,2);
         $commission = $this->query->amount - $cash;
         $fina_set = ['income'=>2,
                      'type'=>7,
                      'record_type'=>1,
                      'cash'=>$cash,
                      'after_cash'=>$user_info->integral - $this->query->amount,
                      'commission'=>$commission,
                      'status'=>3,
                      'user_id'=>$user_id,
                      'bank_info'=>$accountJson];
         $fina = FinaIntegralRegRecordRepository::getInstance();
         $res = $fina->createFina($fina_set,$user_id);
         if(!$res)
         {
             throw new ParamsException([
                 'error_code' => '1000',
                 'message'    => '提现失败，服务内部异常',
             ]);
         }
        DB::commit();
        return apiResponse();
    }
}
